/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dao.NumeracaoDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.NumeracaoMODEL;
import table.numeracao.NumeracaoTable;

/**
 *
 * @author Welisson
 */
public class NumeracaoController extends GenericController{
    public boolean salvar(int de, int ate, NumeracaoTable ntbl){
        NumeracaoMODEL num = new NumeracaoMODEL();
        num.setDe(de);
        num.setAte(ate);
        try {
            int id = new NumeracaoDAO().insert(num);
            if(id == -1){
                return false;
            }else{
                num.setId(id);
                ntbl.addNumeracao(num);
                return true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
            return false;
        }
        
    }
    
    public boolean editar(int id, int de, int ate, NumeracaoTable ntbl, int linhaSelecionada){
        NumeracaoMODEL num = new NumeracaoMODEL();
        num.setId(id);
        num.setDe(de);
        num.setAte(ate);
        
        if(new NumeracaoDAO().update(num)){
            ntbl.updateNumeracao(linhaSelecionada, num);
            return true;
        }
        return false;
        
    }
    
    public boolean excluir(int id, NumeracaoTable ntbl, int linhaSelecionada){
        if(new NumeracaoDAO().delete(id)){
            ntbl.removeNumeracao(linhaSelecionada);
            return true;
        }
        return false;
        
    }
    
    public List<NumeracaoMODEL> listar(){
        try {
            return new NumeracaoDAO().findNumeracao();
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
            List<NumeracaoMODEL> cor = new ArrayList<NumeracaoMODEL>(); 
            return cor;
        }
        
    }
}
